<!-- 新增编辑 -->
<template>
  <div>
    <zm-panel :title="panelTitle" :visible.sync="value" @change="close">
      <zm-form label-width="100px" :model="form" :rules="formRule" ref="zmForm" size="small">
        <zm-form-item label="岗位名称" prop="name">
          <el-input v-model="form.name" :maxlength="20" clearable class="w320" placeholder="请输入岗位名称,不超过20个字" />
        </zm-form-item>
        <zm-form-item prop="dateAuth" label="数据权限">
          <zm-data-radio v-model="form.dateAuth" :list="DATE_AUTH_TYPE"></zm-data-radio>
        </zm-form-item>
        <zm-form-item prop="companyId" label="所属公司" v-if="form.dateAuth === '4'">
          <!-- api="getCompanyList" -->
          <zm-data-select @change="companyChange" @clear="onClear" v-model="form.companyId" api="getCompanyList" value-key="companyId" label-name="companyName" class="w320" placeholder="请选择所属公司"></zm-data-select>
        </zm-form-item>
        <zm-form-item label="指定部门" prop="dateApartmentId" v-if="form.dateAuth === '4'&&form.companyId&&departmentList.length">
          <tree-select v-model="form.dateApartmentId" :tree="departmentList" :is-checkbox="true"></tree-select>
        </zm-form-item>
        <zm-form-item label="菜单权限" prop="menuIds">
          <permission-tree v-model="form.menuIds" :tree="menuList" :tree-props="{label:'menuName',valueKey:'menuId'}" :is-check-all="!rowInfo.id"></permission-tree>
        </zm-form-item>
      </zm-form>
      <div slot="footer" class="tr">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </zm-panel>
  </div>
</template>

<script>
import panelMixin from '@/mixins/panelMixin'
import TreeSelect from '@/components/TreeSelect'
import PermissionTree from '@/components/PermissionTree'
import { DATE_AUTH_TYPE } from '@/model/type'
import { formRule } from '../const'
import systemRoleApis from '@/api/src/systemRole'
import systemMenu from '@/api/src/systemMenu'
import publicApis from '@/api/src/public'
import { loadFromLocal } from '@/utils/auth'
import {SidebarMenuKey} from '@/config'

export default {
  components: {
    PermissionTree,
    TreeSelect
  },
  mixins: [panelMixin],
  props: {
    rowInfo: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        companyId: '',
        name: '',
        menuIds: '',
        dateAuth: '1',
        dateApartmentId: ''// 指定部门Id
      },
      // companyList:[],
      DATE_AUTH_TYPE,
      loading: false,
      formRule: formRule,
      departmentList: [],
      menuList: []
    }
  },
  computed: {
    panelTitle () {
      return this.rowInfo.id ? '编辑岗位' : '新增岗位'
    }
  },
  created () {
    // this.getCompanyListFn()
    this.getPermissionTree()
    if (this.rowInfo.id) {
      this.getDetail()
    }
  },
  methods: {
    // async getCompanyListFn() {
    //   this.companyList = await publicApis.getCompanyList()
    // },
    async getDepartment () {
      this.departmentList = []
      const _res = await publicApis.apartmentList({
        companyId: this.form.companyId
      })
      this.departmentList = _res
    },
    companyChange() {
      this.form.dateApartmentId = ''
      this.getDepartment()
    },
    onClear() {
      this.form.dateApartmentId = ''
    },
    async getPermissionTree () {
      let _res = []
      const asyncMenu = loadFromLocal(SidebarMenuKey)
      let r = []
      asyncMenu.forEach(item => {
        item.children && item.children.map(ii => {
          if (ii.hidden) {
            r.push(ii)
          }
        })
      })
      _res = await systemMenu.getMenuTree({})
      r.forEach(item => {
        _res.forEach(item1 => {
          item1.children && item1.children.map(ii => {
            if (ii.menuUrl === item.name) {
              ii.disabled = item.hidden
            }
          })
        })
      })
      this.menuList = _res || []
    },
    async getDetail () {
      const _res = await systemRoleApis.getInfo({ id: this.rowInfo.id })
      this.form = _res
      this.getPermissionTree()
      this.getDepartment()
    },
    async submit () {
      let _valid = false
      this.$refs.zmForm.validate(valid => {
        _valid = valid
      })
      if (!_valid) return
      const params = JSON.parse(JSON.stringify(this.form))
      if (this.rowInfo.id) {
        params.id = this.rowInfo.id
        await systemRoleApis.update(params)
      } else {
        await systemRoleApis.create(params)
      }
      this.$message.success('操作成功')
      this.$emit('after-save')
      this.close()
    }
  }

}
</script>
